/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.dli;

public class Term {

	private String string = null;
	
	protected Term(){}
	
	public static Term newInstance(String string) throws DliParseException {
		if (string.startsWith("$B ")) {
			return Variable.newInstance(string);
		}
		if (string.startsWith("$A ")) {
			return LocusVar.newInstance(string);
		}
		if (string.startsWith("$private_functor ")) {
			return PrivateFunctor.newInstance(string);
		}
		if (string.startsWith("$the")){
			return The.newInstance(string);
		}
		if (string.startsWith("$fraenkel")){
			return Fraenkel.newInstance(string);
		}
		if (string.startsWith("$var")){
			return Var.newInstance(string);
		}
		if (string.startsWith("$N ")) {
			return Num.newInstance(string);
		}
		int position = string.indexOf("(");
		int positionFunc = string.indexOf(":func ");
		if (position != -1 && positionFunc < position) {
			return Func.newInstance(string); 
		}
		if (position == -1 && positionFunc != -1) {
			return Func.newInstance(string); 
		}
		int positionAggr = string.indexOf(":aggr ");
		if (position != -1 && positionAggr < position) {
			return Aggr.newInstance(string); 
		}
		if (position == -1 && positionAggr != -1) {
			return Aggr.newInstance(string); 
		}
		int positionSel = string.indexOf(":sel ");
		if (position != -1 && positionSel < position) {
			return Sel.newInstance(string); 
		}
		if (position == -1 && positionSel != -1) {
			return Sel.newInstance(string); 
		}
		
		Term term = new Term();
		term.string = string;
		return term;
	}

	@Override
	public String toString() {
		return "[[Term]"+string+"]";
	}
	
}
